package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import javax.swing.JPanel;

import model.quadtree.graph.Node2Graph;

@SuppressWarnings({"serial","rawtypes"})
public class FieldPanel extends JPanel{
	private static int unit = 10;
	private List<Rectangle> obstacles;
	private Hashtable leafNodes;
	private int firstX;
	private int firstY;
	private int x;
	private int y;
	private int width;
	private int height;
	private Point startPoint;
	private Point endPoint;
	private boolean clicked = false;
	
	public FieldPanel() {
		this.setBackground(Color.BLACK);
		obstacles = new ArrayList<Rectangle>();
		leafNodes = new Hashtable();
	}
	
	public void setWidth(int width) {
		this.width = width;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public void mousePressed(int x, int y){
		if(!clicked){
			this.x = firstX = x;
			this.y = firstY = y;
			clicked = !clicked;
		}
		else{
			this.x = x<firstX ? x : firstX;
			this.y = y<firstY ? y : firstY;
			width = Math.abs(x-firstX);
			height = Math.abs(y-firstY);
		}
		System.out.println("("+this.x+","+this.y+")"+"\twidth:"+this.width+" height:"+this.height);
		System.out.println("clicked:"+clicked);
		super.repaint();
	}
	
	public int getRectX() {
		return x;
	}

	public int getRectY() {
		return y;
	}

	public int getRectWidth() {
		return width;
	}

	public int getRectHeight() {
		return height;
	}
	
	public void paintRects(Rectangle rect){
		obstacles.add(rect);
		super.repaint();
	}
	
	public void paintStartPoint(Point startPoint){
		this.startPoint = startPoint;
		super.repaint();
	}
	
	public void paintEndPoint(Point endPoint){
		this.endPoint = endPoint;
		super.repaint();
	}
	
	public void paintQuadtreeCells(Hashtable leafNodes){
		this.leafNodes = leafNodes;
		super.repaint();
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.setColor(Color.darkGray);
        for (int x = 0; x < this.getWidth(); x += unit) {
            g.drawLine(x, 0, x, this.getHeight());
        }
        for (int y = 0; y < this.getHeight(); y += unit) {
            g.drawLine(0, y, this.getWidth(), y);
        }
        g.setColor(Color.WHITE);
        for(Rectangle rect : obstacles)
        	g.fillRect(rect.x, rect.y, rect.width, rect.height);
        g.setColor(Color.WHITE);
        g.fillRect(x, y, width, height);
        g.setColor(Color.RED);
        Enumeration elements = leafNodes.elements();
        Node2Graph current;
		while(elements.hasMoreElements()){
			current = (Node2Graph) elements.nextElement();
			g.drawRect(current.getVert2().x, current.getVert1().y, 
					current.getVert1().x-current.getVert2().x, 
					current.getVert2().y-current.getVert1().y);
			g.drawString(String.valueOf(current.getId()), current.getCenter().x, current.getCenter().y);
		}
		g.setColor(Color.GREEN);
		if(startPoint!=null)
			g.fillRect(startPoint.x, startPoint.y, 20, 20);
		g.setColor(Color.YELLOW);
		if(endPoint!=null)
			g.fillRect(endPoint.x, endPoint.y, 20, 20);
	}
	
	public void setClicked(boolean clicked){
		this.clicked = clicked;
	}
}
